﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mitasoft.Books.Client.Desktop.Core;
using System.Collections;

namespace mitasoft.Books.Client.Desktop.Services
{
    internal class ApplicationMessagesService
    {
        #region fields

        public delegate void ApplicationMessagesEventHandler(object sender, ApplicationMessageEventArgs e);
        public event ApplicationMessagesEventHandler OnMessageAdded;

        private Stack<string> messageStack;

        #endregion

        #region ctor
        public ApplicationMessagesService()
        {
            messageStack = new Stack<string>();
        }
        #endregion

        #region public methods
        
        public void AddMessage(string message, ApplicationMessageEnum type)
        {
            string msg = "";

            if (type == ApplicationMessageEnum.info)
                msg = string.Format("*** {0} : {1}", DateTime.Now.ToString(), message);
            else if (type == ApplicationMessageEnum.error)
                msg = string.Format("\t[ERROR] {0} : {1}", DateTime.Now.ToString(), message);


            ICollection col = messageStack as ICollection;
            lock (col.SyncRoot)
            {
                messageStack.Push(msg);
            }
            //Stack.Synchronized(messageStack).Push(msg);

            ApplicationMessageEventArgs ee = new ApplicationMessageEventArgs { Messages = messageStack.ToList() };

            if (OnMessageAdded != null)
                OnMessageAdded(this, ee);
        }

        public void ClearMessagesStack()
        {
            messageStack.Clear();
        }
        
        #endregion
    }


}
